.TH std::add_cv,std::add_const,std::add_volatile 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::add_cv,std::add_const,std::add_volatile \- std::add_cv,std::add_const,std::add_volatile

.SH Synopsis
   Defined in header <type_traits>
   template< class T >             \fB(1)\fP \fI(since C++11)\fP
   struct add_cv;
   template< class T >             \fB(2)\fP \fI(since C++11)\fP
   struct add_const;
   template< class T >             \fB(3)\fP \fI(since C++11)\fP
   struct add_volatile;

   Provides the member typedef type which is the same as T, except it has a
   cv-qualifier added (unless T is a function, a reference, or already has this
   cv-qualifier)

   1) adds both const and volatile
   2) adds const
   3) adds volatile

   If the program adds specializations for any of the templates described on this page,
   the behavior is undefined.

.SH Member types

   Name Definition
   type the type T with the cv-qualifier

.SH Helper types

   template< class T >                                     \fI(since C++14)\fP
   using add_cv_t       = typename add_cv<T>::type;
   template< class T >                                     \fI(since C++14)\fP
   using add_const_t    = typename add_const<T>::type;
   template< class T >                                     \fI(since C++14)\fP
   using add_volatile_t = typename add_volatile<T>::type;

.SH Possible implementation

   template<class T> struct add_cv { typedef const volatile T type; };

   template<class T> struct add_const { typedef const T type; };

   template<class T> struct add_volatile { typedef volatile T type; };

.SH Notes

   These transformation traits can be used to establish non-deduced contexts in
   template argument deduction:

 template<class T>
 void f(const T&, const T&);

 template<class T>
 void g(const T&, std::add_const_t<T>&);

 f(4.2, 0); // error, deduced conflicting types for 'T'
 g(4.2, 0); // OK, calls g<double>

.SH Example


// Run this code

 #include <iostream>
 #include <type_traits>

 struct foo
 {
     void m() { std::cout << "Non-cv\\n"; }
     void m() const { std::cout << "Const\\n"; }
     void m() volatile { std::cout << "Volatile\\n"; }
     void m() const volatile { std::cout << "Const-volatile\\n"; }
 };

 int main()
 {
     foo{}.m();
     std::add_const<foo>::type{}.m();
     std::add_volatile<foo>::type{}.m();
     std::add_cv<foo>::type{}.m();
 }

.SH Output:

 Non-cv
 Const
 Volatile
 Const-volatile

.SH See also

   is_const        checks if a type is const-qualified
   \fI(C++11)\fP         \fI(class template)\fP
   is_volatile     checks if a type is volatile-qualified
   \fI(C++11)\fP         \fI(class template)\fP
   remove_cv
   remove_const
   remove_volatile removes const and/or volatile specifiers from the given type
   \fI(C++11)\fP         \fI(class template)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   as_const        obtains a reference to const to its argument
   \fI(C++17)\fP         \fI(function template)\fP
